<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑菜单</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>
<body>
    <form class="layui-form" action="" lay-filter="menu-form">
        <div class="top">
            <div class="button-container">
                <div style="padding: 10px;">
                    <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="menu-save">
                        <i class="layui-icon layui-icon-ok"></i>
                        提交
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </div>
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单</label>
                    <div class="layui-input-block">
                        <select name="parent_id" lay-verify="" lay-search>
                            <option value="0">顶级菜单</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="type" value="0" title="目录" checked>
                        <input type="radio" name="type" value="1" title="菜单">
                        <input type="radio" name="type" value="2" title="按钮">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入菜单名称"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单图标</label>
                    <div class="layui-input-block">
                        <input type="text" name="icon" autocomplete="off" placeholder="请输入菜单图标" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">路由名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" autocomplete="off" placeholder="请输入路由名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">路由地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="path" autocomplete="off" placeholder="请输入路由地址" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">组件路径</label>
                    <div class="layui-input-block">
                        <input type="text" name="component" autocomplete="off" placeholder="请输入组件路径" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限标识</label>
                    <div class="layui-input-block">
                        <input type="text" name="permission" autocomplete="off" placeholder="请输入权限标识" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序号</label>
                    <div class="layui-input-block">
                        <input type="number" name="sort" value="0" autocomplete="off" placeholder="请输入排序号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">是否显示</label>
                    <div class="layui-input-block">
                        <input type="radio" name="visible" value="1" title="显示" checked>
                        <input type="radio" name="visible" value="0" title="隐藏">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="正常" checked>
                        <input type="radio" name="status" value="0" title="禁用">
                    </div>
                </div>
            </div>
        </div>
    </form>
    <script src="/component/layui/layui.js"></script>
    <script src="/component/pear/pear.js"></script>
    <script>
        layui.use(['form', 'jquery', 'popup'], function() {
            let form = layui.form;
            let $ = layui.jquery;
            let popup = layui.popup;

            // 加载菜单树数据
            $.ajax({
                url: '/api/menu/tree',
                dataType: 'json',
                type: 'get',
                success: function(result) {
                    if (result.code == 0) {
                        let menus = result.data;
                        let select = $('select[name="parent_id"]');
                        renderMenuOptions(select, menus, 0);
                        form.render('select');
                    }
                }
            });

            // 递归渲染菜单选项
            function renderMenuOptions(select, menus, level) {
                menus.forEach(function(menu) {
                    let prefix = new Array(level + 1).join('├─ ');
                    select.append(new Option(prefix + menu.title, menu.id));
                    if (menu.children && menu.children.length > 0) {
                        renderMenuOptions(select, menu.children, level + 1);
                    }
                });
            }

            let id = getUrlParam("id");
            if (id != null) {
                $.ajax({
                    url: '/api/menu/' + id,
                    dataType: 'json',
                    type: 'get',
                    success: function(result) {
                        if (result.code == 0) {
                            form.val('menu-form', result.data);
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            }

            form.on('submit(menu-save)', function(data) {
                // 确保 parent_id 是数字类型
                data.field.parent_id = parseInt(data.field.parent_id);
                let loading = layer.load();
                $.ajax({
                    url: id == null ? '/api/menu' : '/api/menu/' + id,
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: id == null ? 'post' : 'put',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            popup.success(result.msg, function() {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                                parent.window.refresh();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
                return false;
            });
        });

        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }
    </script>
    <style>
    .top {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        background-color: #fff;
        box-shadow: 0 1px 4px rgba(0,21,41,.08);
        z-index: 999;
    }
    .mainBox {
        margin-top: 55px;
        padding: 15px;
    }
    .main-container {
        background: #fff;
        padding: 15px 30px;
        border-radius: 4px;
        box-shadow: 0 1px 4px rgba(0,21,41,.08);
    }
    .button-container {
        text-align: left;
        padding: 0 15px;
    }
    .layui-btn {
        padding: 0 25px;
        height: 35px;
        line-height: 35px;
        margin: 0 5px;
    }
    .layui-form-item {
        margin-bottom: 25px;
        position: relative;
    }
    .layui-input {
        height: 38px;
        line-height: 38px;
        border-radius: 4px;
    }
    </style>
</body>
</html> 